﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Beeldbankmvc.Code
{
    public class Utility
    {
        public static int getIdFromTitle(String id)
        {
            // check if the id is text_0001 of just 0001
            if (id.Contains("_"))
            {
                String[] parts = id.Split('_');
                string numberPart = parts[parts.Length - 1];
                return Convert.ToInt32(numberPart);
            }
            else
            {
                return Convert.ToInt32(id);
            }
        }

        public static string GetSiteRoot()
        {
            string port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
            if (port == null || port == "80" || port == "443")
                port = "";
            else
                port = ":" + port;

            string protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
            if (protocol == null || protocol == "0")
                protocol = "http://";
            else
                protocol = "https://";

            string sOut = protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + port + System.Web.HttpContext.Current.Request.ApplicationPath;

            return sOut;
        }
    }
}